<template>

  <div>
    <el-upload
      v-loading="loading"
      action="#"
      list-type="picture-card"
      :file-list="fileList"
      :http-request="handleRequest"
      :on-change="changeFile"
      :on-remove="handleRemove"
      :on-preview="handlePreview"
      :limit="limit"
      :class="fileList.length===limit ?'hideAdd':''"
      :before-upload="beforeUpload"
      element-loading-text="上传中"
      element-loading-spinner="el-icon-loading"
      element-loading-background="rgba(0, 0, 0, 0.8)"
    >
      <i class="el-icon-plus" /></el-upload>
    <el-dialog title="图片展示" :visible.sync="visibleDialog">
      <img :src="imgSrc" alt="">
    </el-dialog>
  </div>

</template>

<script>
import COS from 'cos-js-sdk-v5'
const cos = new COS({
  SecretId: 'AKID681iCWs67zkMn371g7jBQSJ1EF1ykIbg',
  SecretKey: 'zlUaUEl6QkxxmSxDF4iBmCSaaR5asxuj'
})
console.log(cos)

export default {
  name: 'ImageUpload',
  components: {},
  props: {
    // eslint-disable-next-line
    onrequestSelf: Function,
    limit: {
      type: Number,
      default: 1
    },
    // eslint-disable-next-line
    beforeUploadSelf: Function,
    // eslint-disable-next-line
    onSuccess: Function

  },
  data() {
    return {
      fileList: [],
      visibleDialog: false,
      imgSrc: '',
      loading: false
    }
  },
  computed: {},
  watch: {},
  created() {},
  methods: {
    changeFile(file, fileList) {
      this.fileList = fileList
    },
    handleRequest(data) {
      console.log('data', data)
      if (this.onrequestSelf) {
        this.onrequestSelf(data)
        return
      }
      this.loading = true
      cos.putObject({
        Bucket: 'itheima35pan-1314927256', /* 填入您自己的存储桶，必须字段 */
        Region: 'ap-shanghai', /* 存储桶所在地域，例如ap-beijing，必须字段 */
        Key: data.file.name, /* 存储在桶里的对象键（例如1.jpg，a/b/test.txt），必须字段 */
        StorageClass: 'STANDARD',
        Body: data.file, // 上传文件对象
        onProgress: function(progressData) {
          // console.log(JSON.stringify(progressData))
          // 这个就是上传的百分比
          // params.percent 表示当前上传的进度
        }
      }, (err, data) => {
        console.log(err || data)
        if (err || data.statusCode !== 200) {
          this.$message.error('图片没有上传成功')
        } else {
          // console.log('https://' + data.Location)

          this.onSuccess && this.onSuccess({ url: 'https://' + data.Location })
        }
        this.loading = false
      })
    },
    handleRemove(file, fileList) {
      this.fileList = fileList
    },
    handlePreview(file) {
      this.visibleDialog = true
      this.imgSrc = file.url
    },
    beforeUpload(file) {
      if (this.beforeUploadSelf) {
        return this.beforeUploadSelf(file)
      }
      console.log(file)
      const fliters = ['image/jpeg', 'image/gif', 'image/bmp', 'image/png']
      if (!fliters.includes(file.type)) {
        this.$message.warning('图片格式不正确 ,请重新上传')
        return false
      }
      if (file.size > 5 * 1024 * 1024) {
        this.$message.warning('图片大小最大不能超过5M')
        return false
      }
      return true
    }

  }
}
</script>

<style lang="scss" scoped>
::v-deep .el-dialog__body{
  text-align: center;
}
.hideAdd {
  ::v-deep .el-upload--picture-card {
    display: none;
  }
}

</style>

